<?php
session_start();
require_once 'inc/config.inc.php';;
require_once 'library/global.func.php';
require_once 'inc/smarty.inc.php';

checkLogin();
checkAgent($objSmarty);

if($_REQUEST['module']=='add'){
	if($_SESSION['randValidadd']==$_POST['addunum']){
			$uins = $_POST['getuins'];
			$rmid =$_POST['rmid'];
			$meth = $_POST['radiobutton'];
			$uin = explode(',',$uins);
			$dbuin = array();
			switch ($meth){
				case "anyonce":
					$sql = 'update '.DB_NAME_ROOMS.'.rooms  set member=0 where id='.$rmid;
					if($result=$conn->Execute($sql)){
						echo 1;exit;
					}else {
						echo 0;exit;
					}
				break;
				case "destList": 
					if(in_array(0,$uin)){
						$sql = 'update '.DB_NAME_ROOMS.'.rooms  set member=0 where id='.$rmid;
						$result=$conn->Execute($sql);
						if(empty($result)){
							echo 0;exit;
						}
						$sql = 'delete from '.DB_NAME_ROOMS.'.roommember_tbl  where room_id="'.$rmid.'"';
						$result = $conn->Execute($sql);
						if(empty($result)){
							echo 50;exit;
						}else{
							echo 1;exit;
						}
						
					}
					$sql='select * from '.DB_NAME_ROOMS.'.roommember_tbl where room_id="'.$rmid.'"';
					if($result = $conn->Execute($sql)){
						while ($arrRow=$result->FetchRow()){
							$dbuin[] = $arrRow['uin'];
						}
					}

					if(empty($result)){
						echo 10;exit;
					}
					$filter['add']=array_diff($uin,$dbuin);
					$filter['del']=array_diff($dbuin,$uin);
					foreach ($filter['del'] as $v ){
						$sql = 'delete from '.DB_NAME_ROOMS.'.roommember_tbl  where room_id="'.$rmid.'" and uin="'.$v.'"';
						$result = $conn->Execute($sql);
					}
					if(empty($result)){
						echo 20;exit;
					}
					$sql = 'update '.DB_NAME_ROOMS.'.rooms  set member=1 where id="'.$rmid.'"';
					$result = $conn->Execute($sql);
					if(empty($result)){
						echo 30;exit;
					}
					foreach ($filter['add'] as $v){
						$sql ='insert into '.DB_NAME_ROOMS.'.roommember_tbl (room_id, uin)	values("'.$rmid.'","'.$v.'")';
						$result=$conn->Execute($sql);
					}
					if(!empty($result)){
						echo 1;exit;
					}else{
						echo 40;exit;
					}
					break;
				
				case "blacklist":
					if(in_array(0,$uin)){
						$sql = 'update '.DB_NAME_ROOMS.'.rooms  set member=1 where id='.$rmid;
						$result = $conn->Execute($sql);
						if(empty($result)){
							echo 0;exit;
						}
						$sql = 'delete from '.DB_NAME_ROOMS.'.roommember_tbl where room_id="'.$rmid.'"';
						$result = $conn->Execute($sql);
						if(empty($result)){
							echo 0;exit;
						}else{
							echo 1;exit;
						}

					}
					$sql='select * from '.DB_NAME_ROOMS.'.roomdeny_tbl where room_id="'.$rmid.'"';
					if($result = $conn->Execute($sql)){
						while ($arrRow=$result->FetchRow()){
							$dbuin[] = $arrRow['uin'];
						}
					}
					$filter['add']=array_diff($uin,$dbuin);
					$filter['del']=array_diff($dbuin,$uin);
					foreach ($filter['del'] as $v ){
						$sql = 'delete from '.DB_NAME_ROOMS.'.roomdeny_tbl where room_id="'.$rmid.'" and uin="'.$v.'"';
						$result = $conn->Execute($sql);
					}
					if(empty($result)){
						echo 0;exit;
					}
					$sql = 'update '.DB_NAME_ROOMS.'.rooms  set member=1 where id='.$rmid;
					$result = $conn->Execute($sql);
					if(empty($result)){
						echo 0;exit;
					}
					foreach ($filter['add'] as $v){
						$sql ='insert into '.DB_NAME_ROOMS.'.roomdeny_tbl (room_id, uin) values("'.$rmid.'","'.$v.'")';
						$result=$conn->Execute($sql);
					}
					if(!empty($result)){
						echo 1;exit;
					}else{
						echo 0;exit;
					}
					break;
			}
		}else {
		echo 2;exit;
	   }

}else if($_REQUEST['module']=='addmanage'){

	if($_SESSION['randValidmanage']==$_POST['addmanageunum']){

		$uins = $_POST['manuins'];
		$rmid = $_POST['manrmid'];
		$uin = explode(',',$uins);
		if(in_array(0,$uin)){
			$sql = 'delete from '.DB_NAME_ROOMS.'.roommanager_tbl where room_id="'.$rmid.'"';
			$result = $conn->Execute($sql);
			if(empty($result)){
				echo 50;exit;
			}else{
				echo 1;exit;
			}
							
		}
		$sql='select * from '.DB_NAME_ROOMS.'.roommanager_tbl where room_id="'.$rmid.'"';
		if($result = $conn->Execute($sql)){
			while ($arrRow=$result->FetchRow()){
				$dbuin[] = $arrRow['uin'];
			}
		}
		if(empty($result)){
			echo 10;exit;
		}
		$filter['add']=array_diff($uin,$dbuin);
		$filter['del']=array_diff($dbuin,$uin);
		foreach ($filter['del'] as $v ){
			$sql = 'delete from '.DB_NAME_ROOMS.'.roommanager_tbl where room_id="'.$rmid.'" and uin="'.$v.'"';
			$result = $conn->Execute($sql);
		}
		if(empty($result)){
			echo 20;exit;
		}
	
		foreach ($filter['add'] as $v){
			$sql ='insert into '.DB_NAME_ROOMS.'.roommanager_tbl (room_id, uin)	values("'.$rmid.'","'.$v.'")';
			$result=$conn->Execute($sql);
		}
		if(!empty($result)){
			echo 1;exit;
		}else{
			echo 40;exit;
		}
	}else {
		echo 2;exit;
	}

}elseif ($_REQUEST['module']=='another'){

	if($_SESSION['randValidoanother']==$_POST['anothernum']){
		$roomid=$_POST['anrmid'];
		$sql='update test_'.DB_NAME_ROOMS.'.rooms.'.DB_NAME_ROOMS.'.rooms set ';
		if(isset($_POST['startpwd'])){
			if($_POST['startpwd']!='' && $_POST['startpwd']==1){
				$passwd = $_POST['passwd'];
				$sql='update '.DB_NAME_ROOMS.'.rooms set passwd="'.$passwd.'",';
				
			}else if($_POST['startpwd']==0){
				$passwd = '';
				$sql='update '.DB_NAME_ROOMS.'.rooms set passwd="'.$passwd.'",';
			}		
		}
		if(isset($_POST['status'])){
			$status = $_POST['status'];
			$sql.='status="'.$status.'",';
		}
		if(isset($_POST['publictalk'])){
			$publictalk=$_POST['publictalk'];
			$sql.='publictalkstat="'.$publictalk.'",';
		}
		if(isset($_POST['privatetalk'])){
			$privatetalkstat=$_POST['privatetalk'];
			$sql.='privatetalkstat="'.$privatetalkstat.'",';
		}
		if(isset($_POST['mikedelay'])){
			$mikedelay = $_POST['mikedelay'];
			$sql.='mikedelay="'.$mikedelay.'",';
		}
		if(isset($_POST['mediadelay'])){
			$mediadelay = $_POST['mediadelay'];
			$sql.='mediadelay="'.$mediadelay.'",';
		}
		if(isset($_POST['songmaxnum'])){
			$songmaxnum=$_POST['songmaxnum'];
			$sql.='songmaxnum="'.$songmaxnum.'",';
		}
		if(isset($_POST['maxshowtime'])){
			$maxshowtime=$_POST['maxshowtime'];
			$sql.='maxshowtime="'.$maxshowtime.'",';
		}
		if(isset($_POST['autoplay'])){
			$autoplay=$_POST['autoplay'];
			$sql.='autoplaystat="'.$autoplay.'"';
		}
		$sql.='where id="'.$roomid.'"';
		if($result = $conn->Execute($sql)){
			echo 1;exit;
		}else{
			echo 0;exit;
		}
	}else{
		echo 2;exit;
	}

}else{
	$nick = $_SESSION['account_arr']['chk_nick'];
	$uin = $_SESSION ['account_arr']['chk_uin'];
	$sql = 'select id from '.DB_NAME_ROOMS.'.rooms where ownuin ="'. $uin.'"';
	if($result = $conn->Execute($sql)){
		while ($arrRow = $result->FetchRow()){
			$roomids [] = $arrRow['id'];
		}
		$objSmarty->assign('roomid',$roomids);
	if(empty($roomids)){
		header('Location: roomtopen.php');
	}
		$roomid = $roomids[0];
		$re['id']=$roomid;
		$sql='select * from '.DB_NAME_ROOMS.'.roommember_tbl where room_id="'.$roomid.'"';
		if($result=$conn->Execute($sql)){
			while ($arrRow=$result->FetchRow()){
				$re['designate'][]=$arrRow['uin'];
			}
			if(empty($re['designate'])){
				$re['designate']='';
			}
		}
		$sql='select * from '.DB_NAME_ROOMS.'.roomdeny_tbl where room_id="'.$roomid.'"';
		if($result=$conn->Execute($sql)){
			while ($arrRow=$result->FetchRow()){
				$re['blacklist'][]=$arrRow['uin'];
			}
			if(empty($re['blacklist'])){
				$re['blacklist']='';
			}
		}
		$sql='select * from '.DB_NAME_ROOMS.'.roommanager_tbl where room_id="'.$roomid.'"';
		if($result=$conn->Execute($sql)){
			while ($arrRow=$result->FetchRow()){
				$re['manange'][]=$arrRow['uin'];
			}
			if(empty($re['manange'])){
				$re['manange']='';
			}
		}
		$sql='select * from '.DB_NAME_ROOMS.'.rooms where id="'.$roomid.'"';
		if($result=$conn->Execute($sql)){
			while ($arrRow=$result->FetchRow()){
				if(isset($arrRow['passwd'])&&$arrRow['passwd']!=''){
					$re['anthor']['passwd']=$arrRow['passwd'];
					$re['anthor']['passwd1']=1;
				}else{
					$re['anthor']['passwd1']=0;
				}
				$re['anthor']['status']=$arrRow['status'];
				$re['anthor']['publictalk']=$arrRow['publictalkstat'];
				$re['anthor']['privatetalk']=$arrRow['privatetalkstat'];
				$re['anthor']['autoplay']=$arrRow['autoplaystat'];
				$re['anthor']['mikedelay']=$arrRow['mikedelay'];
				$re['anthor']['mediadelay']=$arrRow['mediadelay'];
				$re['anthor']['songmaxnum']=$arrRow['songmaxnum'];
				$re['anthor']['maxshowtime']=$arrRow['maxshowtime'];
			}
		}
		$objSmarty -> assign('reArr',$re);
	}
	$objSmarty ->assign('nick',$nick);	
}

$objSmarty->display('roommange.tpl');
include_once("close.inc.php");
?>